package com.smartpi.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.smartpi.pojo.Common;

/**
 * Servlet implementation class SersorSafeServlet
 */

public class SensorSafeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SensorSafeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		JSONObject json = new JSONObject();
		String info = PythonExecuteor
				.executePython(Common.COMMAND_SAFE_DIGITAL);
		// [0, 1, 0, 0]
		String info2 = info.substring(1, (info.length() - 1));
		String[] values = info2.split(",");

		if (values.length < 4) {
			response.getWriter().println("sensor  error");
			response.getWriter().close();
			return;
		}

		json.put("smoke", values[0].trim());
		json.put("fire", values[1].trim());
		json.put("people", values[2].trim());
		json.put("window", values[3].trim());
		response.getWriter().println(json.toString());
		response.getWriter().close();
	}

}
